import type { ResponsePaginationData} from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { message } from 'antd';
import { PowerParser } from '../utils/utils';
import { xtime } from '@/pages/Device/Locker/utils/utils';
import type { QueryListParams, TableListItem } from './data';
import { request } from 'umi';

/**
 * 获取设备列表
 * @param params
 */
export async function queryLowPowerDeviceList(
  params: QueryListParams,
): Promise<RequestData<TableListItem>> {
  const res: ResponsePaginationData<any> = await request(`/device/listWithLowPower`, {
    params: {
      page: params.page,
      pageSize: params.pageSize,
      deviceNo: params.deviceNo,
      spaceName: params.spaceName,
      zoneId: params.zone?.id,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }

  return {
    success: true,
    total: res.data.total,
    data: res.data.list.map(item => ({
      id: String(item.id),
      deviceNo: item.deviceNo,
      power: PowerParser.parseValue(item.battery),
      lastedStatusChangeTime: xtime(item.stateUpdateAt),
      lastedRefreshTime: xtime(item.updateAt),
      zone: { id: item.zoneId, name: item.zoneName },
      space: {
        id: item.spaceId,
        spaceName: item.spaceName,
      },
    })),
  };
}
